package lero.controller;

import com.github.pagehelper.Constant;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lero.pojo.Admin;
import lero.service.AdminServiceI;
import lero.vo.AdminVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController{

    //定义一个全局的日志记录器，通过LoggerFactory获取
    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);

    /**
     *功能描述:新增用户
     * @date 2018/7/9
     * @param admin ：pojo实体
     * @return java.util.HashMap ：返回map数据
     */
    @RequestMapping("/insert.do")
    @ResponseBody
    public HashMap insert(@RequestBody Admin admin){
        adminServiceI.insert(admin);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }


    @RequestMapping("/select.do")
    @ResponseBody
    public  PageInfo<Admin> select(@RequestBody AdminVo vo){
        PageHelper.startPage(vo.getCurrentPage(),vo.getRows());
        List<Admin> admins = adminServiceI.select(vo);
        PageInfo<Admin> info = new PageInfo<>(admins);
        return info;
    }

    /**
     *功能描述:删除单条数据
     * @date 2018/7/9
     * @param id
     * @return java.util.HashMap ：返回map数据
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public  HashMap delete(@RequestParam("id") int id){
        logger.debug("删除单条数据唯一标识{}",id);
        adminServiceI.delete(id);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }

    /**
     *功能描述:批量删除
     * @date 2018/7/9
     * @param ids  批量删除数据的id组成的数组
     * @return java.util.HashMap ：返回map数据
     */
    @RequestMapping("/deleteMany.do")
    @ResponseBody
    public  HashMap deleteMany(@RequestParam("ids[]") int[] ids){
        logger.debug("批量删除数据标识{}",ids);
        adminServiceI.deleteMany(ids);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }


    /**
     *功能描述: 更改用户状态
     * @date 2018/7/9
     * @param id ：被更改的用户id
     * @param status:被更改后的状态标识
     * @return java.util.HashMap
     */
    @RequestMapping("/changeStatus.do")
    @ResponseBody
    public  HashMap changeStatus(@RequestParam("id") int id,@RequestParam("status") String status){
        adminServiceI.changeStatus(id,status);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }


    /**
     *功能描述:
     * @date 2018/7/10
     * @param id : 查询用户的id
     * @return lero.pojo.Admin
     */
    @RequestMapping("/selectOne.do")
    @ResponseBody
    public  Admin selectOne(@RequestParam("id") int id){
        Admin admin = adminServiceI.selectOne(id);
        return admin;
    }


    @RequestMapping(value="/update.do", method = RequestMethod.POST)
    @ResponseBody
    public  HashMap update(@RequestBody Admin admin){
        adminServiceI.update(admin);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }

    @RequestMapping(value="/login.do", method = RequestMethod.POST)
    @ResponseBody
    public  HashMap login(@RequestParam("username") String username, @RequestParam("password")String password, HttpSession session){
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        Admin login = adminServiceI.login(admin);
        HashMap map = new HashMap();
        if(login!=null){
            session.setAttribute("loginUser",login);
            map.put("status",true);
            map.put("message",lero.common.Constant.SUCCESS.toString());
        }else{
            map.put("status",false);
            map.put("message",lero.common.Constant.ERROR.toString());
        }
        return map;
    }

    @RequestMapping(value="/logout.do")
    public String logout(HttpSession session){
        Admin loginUser = (Admin)session.getAttribute("loginUser");
        HashMap map = new HashMap();
        if(loginUser!=null){
           session.removeAttribute("loginUser");
        }
        return "redirect:/layuicms/page/login/login.html";
    }


    @RequestMapping(value="/showLoginUser.do")
    @ResponseBody
    public  HashMap showLoginUser(HttpSession session){
        Admin loginUser = (Admin)session.getAttribute("loginUser");
        HashMap map = new HashMap();
        if(loginUser!=null){
            map.put("status",true);
            map.put("message",lero.common.Constant.SUCCESS.toString());
            map.put("nickname",loginUser.getNickname());
        }else{
            map.put("status",false);
            map.put("message",lero.common.Constant.ERROR.toString());
        }
        return map;
    }




}
